package cate.game.role.pvp.throne;

import cate.common.table.activity.tbboss.TBBossGroupRow;
import cate.common.table.activity.throne.TBLevelGroupRow;
import cate.common.table.activity.throne.TBTrialSettleRwdRow;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDMail;
import cate.common.table.d.GDThrone;
import cate.game.mail.po.MailBuilder;
import cate.game.pvp.throne.TBGameDataManager;
import cate.game.res.MixRes;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import cate.game.role.mail.Mail;
import cate.game.role.pvp.throne.msg.RoleTBDataResp;
import com.google.common.collect.Lists;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

@NoteClass("玩家王座争霸数据")
public class RoleTB extends FuncOpenChecker {

	@NoteField("经历的赛季")
	public int season;

	@NoteField("经历的阶段")
	public byte period;

	@NoteField(value = "等级组",detail = "选拔赛开始那一刻确定")
	public int levelGroup;

	@NoteField(value = "入选了",detail = "选拔赛开始那一刻确定")
	public boolean picked;

	@NoteField("选拔赛")
	public RoleTBTrial trial;

	@NoteField("淘汰赛")
	public RoleTBKO ko;

	@NoteField("冠军boss")
	public RoleTBChampionBoss championBoss;

	@NoteField("全服boss")
	public RoleTBZoneBoss zoneBoss;


	public void initialize(Role role) {
		super.initialize(role);
		if (trial == null) {
			trial = new RoleTBTrial();
		}
		trial.initialize(role);
		if (ko == null) {
			ko = new RoleTBKO();
		}
		ko.initialize(role);
		if (championBoss == null) {
			championBoss = new RoleTBChampionBoss();
		}
		championBoss.initialize(role);
		if (zoneBoss == null) {
			zoneBoss = new RoleTBZoneBoss();
		}
		zoneBoss.initialize(role);
	}

	@Override
	public void onEnter() {
		super.onEnter();
		synData(true);
	}

	@Override
	public void onDaySpan(boolean silence) {
		trial.onDaySpan(silence);
	}

	@Override
	public int getFuncId() {
		return GDFunc.THRONE_BATTLE;
	}

	@Override
	public void onAfterOpen() {
	}

	public void noticeUpdate(){
		role.sendNow(new RoleTBDataResp(this));
	}


	public void synData(boolean silence) {
		TBGameDataManager dataManager = role.getGame().pvp.throne.data;
		if (!dataManager.isDataReady()) {
			return;
		}
		checkSeason(dataManager.kv.index,silence);
		checkPeriod(dataManager.kv.period,silence);
	}


	private void checkSeason(int curSeason, boolean silence) {
		if (this.season == curSeason) {
			return;
		}
		this.season = curSeason;
		this.period = GDThrone.Period.NULL;
		this.levelGroup = 0;
		this.picked = false;
		this.trial.onSeasonChange();
		this.ko.onSeasonChange();
		this.championBoss.onSeasonChange();
		this.zoneBoss.onSeasonChange();
		if (!silence) {
			noticeUpdate();
		}
	}

	private void checkPeriod(byte curPeriod, boolean silence) {
		boolean change = false;
		while (this.period < curPeriod) {
			this.period++;
			onPeriodChange(period);
			change = true;
		}
		if (change && !silence) {
			noticeUpdate();
		}
	}

	private void onPeriodChange(byte period) {
		switch (period) {
			case GDThrone.Period.TRIAL:
				TBLevelGroupRow row = role.getGame().table.throne.group.getRowByLevel(role.getBase().level);
				if (row != null) {
					this.levelGroup = row.group;
					//判断有没有资格入选
					if (role.getPvp().gradePvp.star >= row.dwsStar) {
						picked = true;
					}
				}
				break;
			case GDThrone.Period.KO:
				if (picked) {
					//结算排行榜段位奖励
					TBTrialSettleRwdRow rwdRow = role.getGame().table.throne.trialSettleRwd.findRow(levelGroup, trial.point);
					if (rwdRow != null) {
						Mail mail = new MailBuilder().setContentId(GDMail.Tid.TB_THRONE_TRIAL_SETTLE_RWD).
								setReward(new MixRes(rwdRow.rwdStr)).setParamList(Lists.newArrayList(trial.point)).build();
						role.getMail().add(mail);
					}
				}
				break;
			case GDThrone.Period.ZONE_BOSS:
				zoneBoss.clear();
				TBBossGroupRow groupRow = role.getGame().table.tbBoss.group.findGroupRow(role.getBase().level);
				if (groupRow != null) {
					zoneBoss.group = groupRow.group;
				}
				break;
			default:
				break;
		}
	}

	public void gmSetGroup(int group){
		this.levelGroup = group;
		this.picked = true;
		noticeUpdate();
	}
}
